package com.lyx.framework.manager;

import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;

import com.lyx.framework.utils.LogUtils;

import java.io.IOException;

import androidx.annotation.NonNull;

public class MediaPlayerManager {

    //播放
    public static final int MEDIA_STATUS_PLAY = 0;
    //暂停
    public static final int MEDIA_STATUS_PAUSE = 1;
    //停止
    public static final int MEDIA_STATUS_STOP = 2;

    public static int MEDIA_STATUS = MEDIA_STATUS_STOP;

    private MediaPlayer mMediaPlayer;
    private IMusicPlayListener mIMusicPlayListener;
    private static final int H_PROGRESS = 1000;

    /**
     * 计算歌曲的进度：
     * 1.开始播放的时候就开启循环计算时长
     * 2.将进度计算结果对外抛出
     */
    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case H_PROGRESS:
                    if (mIMusicPlayListener != null) {
                        //拿到当前时长
                        int currentPosition = getCurrentPosition();
                        mIMusicPlayListener.onProgress(currentPosition);
                        mHandler.sendEmptyMessageDelayed(H_PROGRESS, 1000);
                    }
                    break;
            }
            return false;
        }
    });

    public MediaPlayerManager() {
        mMediaPlayer = new MediaPlayer();
    }

    //是否在播放
    public boolean isPlaying() {
        return mMediaPlayer.isPlaying();
    }

    //开始播放
    public void startPlay(AssetFileDescriptor path) {
        try {
            mMediaPlayer.reset();
            mMediaPlayer.setDataSource(path.getFileDescriptor(), path.getStartOffset(), path.getLength());
            mMediaPlayer.prepare();
            mMediaPlayer.start();
            MEDIA_STATUS = MEDIA_STATUS_PLAY;
            mHandler.sendEmptyMessage(H_PROGRESS);
        } catch (IOException e) {
            LogUtils.getInstance().e(e.toString());
            e.printStackTrace();
        }
    }

    //暂停播放
    public void pausePlay() {
        if (isPlaying()) {
            mMediaPlayer.pause();
            MEDIA_STATUS = MEDIA_STATUS_PAUSE;
            removeHandler();
        }
    }

    //继续播放
    public void continuePlay() {
        if (MEDIA_STATUS == MEDIA_STATUS_STOP || MEDIA_STATUS == MEDIA_STATUS_PLAY) return;
        mMediaPlayer.start();
        MEDIA_STATUS = MEDIA_STATUS_PLAY;
        mHandler.sendEmptyMessage(H_PROGRESS);
    }

    //停止播放
    public void stopPlay() {
        if (isPlaying()) {
            mMediaPlayer.stop();
            MEDIA_STATUS = MEDIA_STATUS_STOP;
            removeHandler();
        }
    }

    //获取当前位置
    public int getCurrentPosition() {
        return mMediaPlayer.getCurrentPosition();
    }

    //获取总时长
    public int getDuration() {
        return mMediaPlayer.getDuration();
    }

    //是否循环
    public void setLooping(boolean isLooping) {
        mMediaPlayer.setLooping(isLooping);
    }

    //跳转位置
    public void seekTo(int ms) {
        mMediaPlayer.seekTo(ms);
    }

    public void setOnCompletionListener(MediaPlayer.OnCompletionListener listener) {
        mMediaPlayer.setOnCompletionListener(listener);
    }

    public void setOnErrorListener(MediaPlayer.OnErrorListener listener) {
        mMediaPlayer.setOnErrorListener(listener);
    }

    public void setOnProgressListener(IMusicPlayListener listener) {
        mIMusicPlayListener = listener;
    }

    public void removeHandler() {
        if (mHandler != null) {
            mHandler.removeMessages(H_PROGRESS);
        }
    }

}
